package com.kingbook.common.file;

import com.kingbook.common.utils.CloseableUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URI;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;

public class LocalFileManager {

    private static final Logger logger = LoggerFactory.getLogger(LocalFileManager.class);

    public static byte[] file2Byte(String filePath) {
        byte[] buffer = null;
        byte[] b = new byte[1024];
        int n;

        BufferedInputStream in = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            in = new BufferedInputStream(new FileInputStream(filePath));
            while ((n = in.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            buffer = bos.toByteArray();
        } catch (IOException e) {
            logger.error("", e);
        } finally {
            CloseableUtils.cleanup(in, bos);
        }
        return buffer;
    }

    public static void byte2File(byte[] buf, String filePath) {
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(filePath));
            bos.write(buf);
        } catch (Exception e) {
            logger.error("", e);
        } finally {
            CloseableUtils.cleanup(bos);
        }
    }


    public static boolean writeFile(String path, String content) {
        try {

            File file = new File(path);
            if (!file.exists())
                file.createNewFile();
            FileWriter fw = new FileWriter(file, true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.flush();
            bw.close();
            return true;
        } catch (IOException e) {
            logger.error("", e);
            return false;
        }
    }


    public static boolean upload(String srcFile, String dstFile) {
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            inChannel = FileChannel.open(Paths.get(URI.create(srcFile)));
            outChannel = FileChannel.open(Paths.get(URI.create(dstFile)));
            inChannel.transferTo(0, inChannel.size(), outChannel);
            return true;
        } catch (IOException e) {
            logger.error("", e);
            return false;
        }
    }

    public static boolean upload(InputStream inputStream, String dstFile) {
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(dstFile);
            byte[] b = new byte[1024];
            while (inputStream.read(b) != -1) {
                outputStream.write(b);
            }
            outputStream.flush();
            return true;
        } catch (IOException e) {
            logger.error("", e);
            return false;
        } finally {
            CloseableUtils.cleanup(inputStream, outputStream);
        }
    }

    public static boolean download(String localFile, String dstFile) {
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            inChannel = FileChannel.open(Paths.get(URI.create(dstFile)));
            outChannel = FileChannel.open(Paths.get(URI.create(localFile)));
            inChannel.transferTo(0, inChannel.size(), outChannel);
            return true;
        } catch (IOException e) {
            logger.error("", e);
            return false;
        } finally {
            CloseableUtils.cleanup(inChannel, outChannel);
        }
    }

    public static FileInputStream download(String dstFile) {
        FileInputStream inputStream = null;
        try {
            File file = new File(dstFile);
            isExisted(file);
            inputStream = new FileInputStream(file);
        } catch (IOException e) {
            logger.error("", e);
        } finally {
            CloseableUtils.cleanup(inputStream);
        }
        return inputStream;
    }

    public static String readFile(String path, String separator) {
        try {
            return readFile(new FileReader(path), separator);
        } catch (Exception e) {
            logger.error("", e);
            return "";
        }
    }

    public static String readFile(File file, String separator) {
        try {
            return readFile(new FileReader(file), separator);
        } catch (Exception e) {
            logger.error("", e);
            return "";
        }
    }

    public static String readFile(FileReader fr, String separator) {
        StringBuffer sb = new StringBuffer();
        String line = "";
        BufferedReader br = null;
        try {
            br = new BufferedReader(fr);
            while ((line = br.readLine()) != null) {
                sb.append(line).append(separator);
            }
        } catch (Exception e) {
            logger.error("", e);
            return "";
        } finally {
            CloseableUtils.cleanup(br);
        }
        return sb.toString();
    }

    public static boolean delete(String name) {
        File file = new File(name);
        return file.delete();
    }

    public static boolean mkdir(String dirName) {
        File file = new File(dirName);
        if (file.exists()) {
            //logger.debug(ErrorMessages.fileIsExisted.value);
            return false;
        } else {
            return file.mkdir();
        }
    }

    public static boolean rename(String oldFile, String newFile) {
        File file = new File(oldFile);
        if (file.exists())
            return file.renameTo(new File(newFile));
        return false;
    }

    public static boolean isExisted(File file) {
        boolean b = file.exists();
        // if (!b)
        //logger.debug(ErrorMessages.fileNotExisted.value);
        return b;
    }

}
